package com.haoshuang.zhouzhoubang.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ItemServiceOrderMemberListBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderDetail2Bean
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.imageResource
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.visible
import com.mufeng.libs.utils.view.visibleOrGone

/**
 * 服务订单队员列表适配器
 * @param data 队员列表集合
 * @param add () -> Unit 添加队员执行方法
 * @param isShowAdd Boolean 是否显示添加按钮
 * @param delete (Int) -> Unit 联系队员点击执行方法
 * @param itemClick (Int, MutableList<LocalMedia>) -> Unit 队员头像点击执行方法
 * */
class ServiceOrderMemberListAdapter(
    val context: Context,
    val data: MutableList<OrderDetail2Bean.TeamMember>,
    val isShowAdd: Boolean,
    val add: () -> Unit,
    val contact: (Int) -> Unit,
    val itemClick: (Int, MutableList<OrderDetail2Bean.TeamMember>) -> Unit,
) : RecyclerView.Adapter<ServiceOrderMemberListAdapter.ChooseMemberViewHolder>() {

    class ChooseMemberViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val binding = ItemServiceOrderMemberListBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChooseMemberViewHolder {
        return ChooseMemberViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_service_order_member_list, parent, false)
        )
    }

    override fun onBindViewHolder(holder: ChooseMemberViewHolder, position: Int) {
        val binding = holder.binding

        // 当前位置大于队员数量并且小于最大值减1
        if (position >= data.size ) {
            // 显示添加图片按钮  并隐藏删除按钮
            if (isShowAdd) {
                binding.rivImage.visible()
                binding.tvContact.gone()
                binding.tvContact2.gone()

                binding.rivImage.imageResource = R.mipmap.icon_tjry
            }else{
                binding.rivImage.gone()
                binding.tvContact.gone()
                binding.tvContact2.gone()
            }

        } else {
            // 显示图片 并显示删除按钮
            binding.rivImage.visible()
            binding.tvContact.visible()
            binding.tvContact2.visible()
            if (data[position].team_is_sign=="0"){
                binding.tvContact2.text="未签到"
            }else{
                binding.tvContact2.text="已签到"
            }
            binding.rivImage.load(data[position].avatar, R.mipmap.default_image)
            binding.tvHead.visibleOrGone(data[position].is_leader == 1)

        }

        binding.tvContact.clickWithTrigger {
            contact(position)
        }

        binding.rivImage.clickWithTrigger {
            if(position >= data.size ){

                add()
            }else{
                itemClick(position, data)
            }
        }
    }

    override fun getItemCount(): Int {
        return if (data.size == 0) 1 else data.size + 1
    }

}